<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

require dirname(__FILE__).'/pc_controller.php';

class Framework extends PC_controller {

	public function __construct(){
		parent::__construct();
		$this->load->model('Frameworkmodel','m_framework');
	}

	public function index(){
		$data = parent::index();
		$this->load->view('framework',$data);
	}

	public function getFrameworkFileList(){
		$assist['where'] = getFilter($this->input->post(),Frameworkmodel::$searchField);
		$assist['like'] = getFilter($this->input->post(),Frameworkmodel::$likeField);
		$list = $this->m_framework->getFrameworkFileList($assist);
		$count = count($list);

		$users = extractColumn($list,'user_id');
		$users = $this->m_user->getUserById($users);
		$users = reindexArray($users,'user_id');
		foreach ($list as &$record) {
			$record['user_name'] = '';
			if(isset($users[$record['user_id']])) $record['user_name'] = $users[$record['user_id']]['user_name'];
		}

		$this->response->setSuccess();
		$this->response->setItems($list);
		$this->response->setTotalCount($count);
		$this->response->responseExit();
	}

	public function createFrameworkFile(){
		$this->validate->setRule('framework_file_name','string');
		$this->validate->setRule('framework_file_name','message',$this->lang->line('error_parameter'));
		if(!$this->validate->run($this->input->post())){
			
			$this->response->setDetail($this->validate->getMessage());
			$this->response->responseExit();
		}

		$info = $this->validate->getResult();
		if(!$this->m_framework->checkNameAvailable($info['framework_file_name'])){
			
			$this->response->setDetail($this->lang->line('error_project_name_used'));
			$this->response->responseExit();
		}

		$info['user_id'] = $this->m_app->getCurrentUserId();
		$info['framework_file_status'] = STATUS_DISABLE;
		$info['framework_file_time_lastmodified'] = NOW;
		$this->m_framework->createFrameworkFile($info);

		$this->response->setSuccess();
		$this->response->setDetail($this->lang->line('success_update'));
		$this->response->responseExit();
	}

	public function editFrameworkFile(){
		$this->validate->setRule('framework_file_id','integer');
		$this->validate->setRule('framework_file_id','message',$this->lang->line('error_parameter'));
		$this->validate->setRule('framework_file_time_lastmodified','string');
		$this->validate->setRule('framework_file_time_lastmodified','message',$this->lang->line('error_parameter'));
		$this->validate->setRule('framework_file_name','string');
		$this->validate->setRule('framework_file_name','optional');
		$this->validate->setRule('framework_file_name','message',$this->lang->line('error_parameter'));
		$this->validate->setRule('framework_file_content','string');
		$this->validate->setRule('framework_file_content','optional');
		$this->validate->setRule('framework_file_content','message',$this->lang->line('error_parameter'));
		if(!$this->validate->run($this->input->post())){
			
			$this->response->setDetail($this->validate->getMessage());
			$this->response->responseExit();
		}

		$info = $this->validate->getResult();
		$frameworkFile = $this->m_framework->getFrameworkFile($info['framework_file_id']);
		if(empty($frameworkFile) || $frameworkFile['framework_file_time_lastmodified'] != $info['framework_file_time_lastmodified']){
			
			$this->response->setDetail($this->lang->line('error_edited'));
			$this->response->responseExit();
		}

		if(!$this->m_framework->checkNameAvailable($info['framework_file_name'],$info['framework_file_id'])){
			
			$this->response->setDetail($this->lang->line('error_project_name_used'));
			$this->response->responseExit();
		}

		$info['framework_file_time_lastmodified'] = NOW;
		$info['user_id'] = $this->m_app->getCurrentUserId();
		$this->m_framework->editFrameworkFile($info['framework_file_id'],$info);
		
		$this->response->setSuccess();
		$this->response->setDetail($this->lang->line('success_update'));
		$this->response->setAddon(array('framework_file_time_lastmodified'=>NOW));
		$this->response->responseExit();
	}

	public function enableFrameworkFile(){
		$this->validate->setRule('framework_file_id','integer');
		$this->validate->setRule('framework_file_id','message',$this->lang->line('error_parameter'));
		if(!$this->validate->run($this->input->post())){
			
			$this->response->setDetail($this->validate->getMessage());
			$this->response->responseExit();
		}

		$info = $this->validate->getResult();
		$this->m_framework->editFrameworkFile($info['framework_file_id'],array(
			'framework_file_status' => STATUS_ACTIVE,
			'framework_file_time_lastmodified' => NOW,
			'user_id' => $this->m_app->getCurrentUserId(),
		));

		$this->response->setSuccess();
		$this->response->setDetail($this->lang->line('success_update'));
		$this->response->responseExit();
	}

	public function disableFrameworkFile(){
		$this->validate->setRule('framework_file_id','integer');
		$this->validate->setRule('framework_file_id','message',$this->lang->line('error_parameter'));
		if(!$this->validate->run($this->input->post())){
			
			$this->response->setDetail($this->validate->getMessage());
			$this->response->responseExit();
		}

		$info = $this->validate->getResult();
		$this->m_framework->editFrameworkFile($info['framework_file_id'],array(
			'framework_file_status' => STATUS_DISABLE,
			'framework_file_time_lastmodified' => NOW,
			'user_id' => $this->m_app->getCurrentUserId(),
		));

		$this->response->setSuccess();
		$this->response->setDetail($this->lang->line('success_update'));
		$this->response->responseExit();
	}

	public function sortFrameworkFile(){
		$this->validate->setRule('framework_file_ids','array');
		$this->validate->setRule('framework_file_ids','message',$this->lang->line('error_parameter'));
		if(!$this->validate->run($this->input->post())){
			
			$this->response->setDetail($this->validate->getMessage());
			$this->response->responseExit();
		}

		$info = $this->validate->getResult();
		$res = array();
		$total = count($info['framework_file_ids']);
		foreach ($info['framework_file_ids'] as $key => $framework_file_id) {
			$res[] = array(
				'framework_file_id' => $framework_file_id,
				'framework_file_time_lastmodified' => NOW,
				'user_id' => $this->m_app->getCurrentUserId(),
				'framework_file_sort' => $total-$key,
			);
		}
		$this->m_framework->editFrameworkFiles($res);

		$this->response->setSuccess();
		$this->response->setDetail($this->lang->line('success_update'));
		$this->response->responseExit();
	}
}
/* End of file framework.php */
/* Location: ./application/controllers/framework.php */